﻿using System;

namespace PublicEntity
{
    /// <summary>
    /// 返回实体基类
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    public class ResponseBaseResult<T> : PublicEntity.IResponseBaseResult<T>
    {
        /// <summary>
        /// 状态
        /// </summary>
        public int Status { get; set; }

        /// <summary>
        /// 提示信息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 数据
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 是否已处理
        /// </summary>
        public bool Bol { get; set; }

        /// <summary>
        /// 默认成功的实例化
        /// </summary>
        public ResponseBaseResult()
        {
            Status = 1000;
            Bol = true;
            Message = "操作成功";
        }

        /// <summary>
        /// 是否成功的实例化
        /// </summary>
        /// <param name="Bol">是否成功</param>
        public ResponseBaseResult(bool Bol)
        {
            this.Bol = Bol;
            if (Bol)
            {
                Status = 1000;
                Message = "操作成功";
            }
            else
            {
                Status = 2000;
                Message = "操作失败";
            }
        }

        /// <summary>
        /// 创建interface
        /// </summary>
        /// <remarks>
        /// interface可以协变T
        /// 也就是说Data可以隐式转换
        /// </remarks>
        /// <returns></returns>
        public static PublicEntity.IResponseBaseResult<T> Create()
        {
            return new ResponseBaseResult<T>();
        }

        /// <summary>
        /// 自定义隐式转换
        /// </summary>
        /// <param name="a"></param>
        public static implicit operator ResponseBaseResult(ResponseBaseResult<T> a)
        {
            return new ResponseBaseResult
            {
                Bol = a.Bol,
                Message = a.Message,
                Data = a.Data,
                Status = a.Status,
            };
        }

        /// <summary>
        /// 自定义隐式转换
        /// </summary>
        /// <param name="a"></param>
        public static implicit operator ResponseBaseResult<T>(ResponseBaseResult a)
        {
            var res = new ResponseBaseResult<T>
            {
                Bol = a.Bol,
                Message = a.Message,
                Status = a.Status,
            };
            if (a.Data != DBNull.Value && a.Data != null && (a.Data is T))
            {
                res.Data = (T)a.Data;
            }
            return res;
        }
    }
}
